package com.thunder.ktv.scoreengine2test.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * Asset文件操作工具类
 */
public class AssetUtils {

    private static final String TAG = "AssetUtils";
    /**
     * 从assets目录读取文件数据
     * @param context 上下文
     * @param assetPath assets中的文件路径
     * @return 文件字节数据
     * @throws IOException 读取文件失败时抛出
     */
    public static byte[] readAssetFile(Context context, String assetPath) {
        // 移除"assets/"前缀，因为AssetManager不需要这个前缀
        assetPath = assetPath.replace("assets/", "");
        
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open(assetPath);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            return data;
        } catch (IOException e) {
            Log.d(TAG, "readAssetFile:读取文件失败", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // 忽略关闭时的异常
                }
            }
        }
        return null;
    }
} 